<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>任务承接管理</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <div th:replace="~{fragments/header :: header}"></div>
    
    <div class="container mt-4">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h2>任务承接管理</h2>
        </div>
        
        <!-- 搜索表单 -->
        <div class="card mb-4">
            <div class="card-body">
                <form th:action="@{/employer/task-applications}" method="get" class="row g-3">
                    <div class="col-md-5">
                        <label for="taskId" class="form-label">任务编号</label>
                        <input type="number" class="form-control" id="taskId" name="taskId" placeholder="输入任务编号" th:value="${searchTaskId}">
                    </div>
                    <div class="col-md-5">
                        <label for="status" class="form-label">承接状态</label>
                        <select class="form-control" id="status" name="status">
                            <option value="">全部状态</option>
                            <option value="not_delivered" th:selected="${searchStatus == 'not_delivered'}">未交付</option>
                            <option value="delivered" th:selected="${searchStatus == 'delivered'}">已交付</option>
                            <option value="confirmed" th:selected="${searchStatus == 'confirmed'}">已确认</option>
                        </select>
                    </div>
                    <div class="col-md-2 d-flex align-items-end">
                        <button type="submit" class="btn btn-primary me-2">搜索</button>
                        <a th:href="@{/employer/task-applications}" class="btn btn-secondary">重置</a>
                    </div>
                </form>
            </div>
        </div>
        
        <!-- 任务承接列表 -->
        <div class="row">
            <div class="col-12">
                <div th:if="${applications != null and #lists.isEmpty(applications)}" class="alert alert-info">
                    暂无任务承接记录
                </div>
                
                <div th:if="${applications != null and not #lists.isEmpty(applications)}">
                    <div class="table-responsive">
                        <table class="table table-striped table-hover">
                            <thead>
                                <tr>
                                    <th>承接编号</th>
                                    <th>任务编号</th>
                                    <th>任务标题</th>
                                    <th>承接威客用户名</th>
                                    <th>承接时间</th>
                                    <th>交付时间</th>
                                    <th>交付状态</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr th:each="taskApplication : ${applications}">
                                    <td th:text="${taskApplication.applicationId}">承接编号</td>
                                    <td th:text="${taskApplication.taskId}">任务编号</td>
                                    <td>
                                        <span th:if="${taskMap != null and taskMap.get(taskApplication.taskId) != null}" 
                                              th:text="${taskMap.get(taskApplication.taskId).title}">
                                            任务标题
                                        </span>
                                        <span th:if="${taskMap == null or taskMap.get(taskApplication.taskId) == null}">
                                            未找到任务标题
                                        </span>
                                    </td>
                                    <td>
                                        <span th:if="${freelancerMap != null and freelancerMap.get(taskApplication.freelancerId) != null}" 
                                              th:text="${freelancerMap.get(taskApplication.freelancerId).username}">
                                            威客用户名
                                        </span>
                                        <span th:if="${freelancerMap == null or freelancerMap.get(taskApplication.freelancerId) == null}">
                                            未找到威客信息
                                        </span>
                                    </td>
                                    <td th:text="${#temporals.format(taskApplication.applyTime, 'yyyy-MM-dd HH:mm')}">承接时间</td>
                                    <td th:text="${#temporals.format(taskApplication.deliveryTime, 'yyyy-MM-dd HH:mm')}">交付时间</td>
                                    <td>
                                        <span th:if="${taskApplication.deliveryStatus == 'not_delivered'}" class="badge bg-primary">未交付</span>
                                        <span th:if="${taskApplication.deliveryStatus == 'delivered'}" class="badge bg-warning">已交付</span>
                                        <span th:if="${taskApplication.deliveryStatus == 'confirmed'}" class="badge bg-success">已确认</span>
                                    </td>
                                    <td>
                                        <a th:href="@{/employer/task-applications/{id}(id=${taskApplication.applicationId})}" class="btn btn-sm btn-primary">查看详情</a>
                                        <!-- 移除了雇主标记为已交付的功能，只保留标记为已确认的功能 -->
                                        <form th:action="@{/employer/task-applications/{id}/mark-confirmed(id=${taskApplication.applicationId})}" method="post" style="display: inline;" th:if="${taskApplication.deliveryStatus == 'delivered'}">
                                            <button type="submit" class="btn btn-sm btn-success">标记为已确认</button>
                                        </form>
                                        <!-- 添加支付任务赏金按钮 -->
                                        <form th:action="@{/employer/task-applications/{id}/pay-bounty(id=${taskApplication.applicationId})}" method="post" style="display: inline;" th:if="${taskApplication.deliveryStatus == 'confirmed'}">
                                            <button type="submit" class="btn btn-sm btn-info">支付任务赏金</button>
                                        </form>
                                        <!-- 添加支付完成后的提示 -->
                                        <span th:if="${taskApplication.deliveryStatus == 'completed'}" class="badge bg-secondary">赏金已支付</span>
                                        <form th:action="@{/employer/task-applications/{id}/delete(id=${taskApplication.applicationId})}" method="post" style="display: inline;">
                                            <button type="submit" class="btn btn-sm btn-danger" 
                                                    onclick="return confirm('确定要删除这个承接记录吗？')">删除</button>
                                        </form>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <div th:replace="~{fragments/footer :: footer}"></div>
</body>
</html>